const reqlib = require('app-root-path').require
const numberUtil = reqlib('/app/util/numberUtil')

function getAbDiff (a, b) {
  const diff = numberUtil.countDifferenceRate(a, b)
  return Math.abs(diff)
}

module.exports = {
  noBuySgSmallBreakThrough () {
    const hourEmaInfo = this.btbItem.hourEmaInfo || {}
    const dayEmaInfo = this.btbItem.dayEmaInfo || {}
    const hour4EmaInfo = this.btbItem.hour4EmaInfo || {}
    if (dayEmaInfo.sg < 1 || hour4EmaInfo.sg < 1) {
      const emInfoList = hourEmaInfo.emInfoList || []
      const rList = [...emInfoList].reverse()
      // 现在
      const a = rList[0]
      // 之前
      const b = rList[4]
      const b3 = rList[3]
      const b2 = rList[2]
      const b1 = rList[1]
      if (a && b) {
        if (
          (a.ema10 > a.ema20 && a.ema5 > a.ema10) &&
          (
            (b.ema10 < b.ema20 && b.ema5 < b.ema10) ||
            (b3.ema10 < b3.ema20 && b3.ema5 < b3.ema10) ||
            (b2.ema10 < b2.ema20 && b2.ema5 < b2.ema10) ||
            (b1.ema10 < b1.ema20 && b1.ema5 < b1.ema10)
          )
        ) {
          this.buy = false
          this.buyReason = ''
        }
      }
    }
  },
  noBuyCloseToLine () {
    const dayEmaInfo = this.btbItem.dayEmaInfo || {}
    const hour4EmaInfo = this.btbItem.hour4EmaInfo || {}
    if (
      (dayEmaInfo.ema10 < dayEmaInfo.ema20 && dayEmaInfo.ema5 < dayEmaInfo.ema10) ||
      (dayEmaInfo.ema5 < dayEmaInfo.ema20 && dayEmaInfo.ema5 > dayEmaInfo.ema10)
    ) {
      if (
        getAbDiff(this.close, dayEmaInfo.ema5) < 0.5 ||
        getAbDiff(this.close, dayEmaInfo.ema10) < 0.5 ||
        getAbDiff(this.close, dayEmaInfo.ema20) < 0.5
      ) {
        this.buy = false
        this.buyReason = ''
      }
    }
    if (
      (hour4EmaInfo.ema10 < hour4EmaInfo.ema20 && hour4EmaInfo.ema5 < hour4EmaInfo.ema10) ||
      (hour4EmaInfo.ema5 < hour4EmaInfo.ema20 && hour4EmaInfo.ema5 > hour4EmaInfo.ema10)
    ) {
      if (
        getAbDiff(this.close, hour4EmaInfo.ema5) < 0.5 ||
        getAbDiff(this.close, hour4EmaInfo.ema10) < 0.5 ||
        getAbDiff(this.close, hour4EmaInfo.ema20) < 0.5
      ) {
        this.buy = false
        this.buyReason = ''
      }
    }
  },
  noBuyTotalInfoBad () {
    // if (this.btbIndexTotalInfo.hourAverageScore < 0) {
    //   this.buy = false
    //   this.buyReason = ''
    // }
    // if (this.btbIndexTotalInfo.dayAverageScore < 0) {
    //   this.buy = false
    //   this.buyReason = ''
    // }
  },
  noBuyTendencyKong () {
    if (this.btbItem.tendency === 'kong') {
      this.buy = false
      this.buyReason = ''
    }
  },
  noBuyIfNewUp () {
    const weekEmaInfo = this.btbItem.weekEmaInfo || {}
    const dayEmaInfo = this.btbItem.dayEmaInfo || {}
    const hour4EmaInfo = this.btbItem.hour4EmaInfo || {}
    const hourEmaInfo = this.btbItem.hourEmaInfo || {}
    const minute15EmaInfo = this.btbItem.minute15EmaInfo || {}
    if (!(weekEmaInfo.isDuo && weekEmaInfo.isAllIncrease)) {
      if (hour4EmaInfo.macdud === 1) {
        this.setNoBuy()
      }
      if (hour4EmaInfo.kdjud === 1) {
        if (!(
          hour4EmaInfo.isLongDuo &&
          hour4EmaInfo.macdud > 4 &&
          hour4EmaInfo.emaKeepDays > 4 &&
          dayEmaInfo.isLongDuo &&
          dayEmaInfo.macdud > 4 &&
          dayEmaInfo.emaKeepDays > 4
        )) {
          this.setNoBuy()
        }
      }
      if (hourEmaInfo.macdud === 1) {
        this.setNoBuy()
      }
      if (hourEmaInfo.kdjud === 1) {
        if (!(
          hourEmaInfo.isLongDuo &&
          hourEmaInfo.macdud > 4 &&
          hourEmaInfo.t10b20KeepDays > 10 &&
          hour4EmaInfo.isLongDuo &&
          hour4EmaInfo.macdud > 4 &&
          hour4EmaInfo.t10b20KeepDays > 10
        )) {
          this.setNoBuy()
        }
      }
      if (minute15EmaInfo.macdud === 1 || minute15EmaInfo.kdjud === 1) {
        if (!(
          minute15EmaInfo.t10b20KeepDays > 10 &&
          hourEmaInfo.isDuo &&
          hourEmaInfo.macdud > 4 &&
          hourEmaInfo.t10b20KeepDays > 10
        )) {
          this.setNoBuy()
        }
      }
    }
    if (
      hour4EmaInfo.nowRatio5 > 0.5 &&
      dayEmaInfo.nowRatio5 > 0.5
    ) {
      this.setNoBuy()
    }
    if (
      minute15EmaInfo.nowRatio5 > 0.5 &&
      hourEmaInfo.days5RealWave < 0.2 &&
      hourEmaInfo.days5MaxWave < 0.15
    ) {
      this.setNoBuy()
    }
    if (
      hourEmaInfo.isReadjust &&
      minute15EmaInfo.days5RealWave < 0.2 &&
      minute15EmaInfo.days5MaxWave < 0.15 &&
      !hourEmaInfo.isDuo
    ) {
      this.setNoBuy()
    }
    if (
      minute15EmaInfo.nowRatio5 > 0.5 &&
      hourEmaInfo.nowRatio5 > 0.5 &&
      this.btbIndexTotalInfo.positionFen < 50 &&
      hourEmaInfo.macd > 0 &&
      hourEmaInfo.macdud < 4 &&
      hourEmaInfo.kdjDiff > 0 &&
      hourEmaInfo.kdjud < 4 &&
      minute15EmaInfo.sg > 4 &&
      hourEmaInfo.sg > 5
    ) {
      this.setNoBuy()
    }
    if (
      this.btbIndexTotalInfo.positionFen < -30 &&
      hourEmaInfo.nowRatio5 > 0.5 &&
      (hourEmaInfo.macdud < 3 || hourEmaInfo.kdjud < 3) &&
      hourEmaInfo.sg > 5 &&
      dayEmaInfo.days5RealWave < 0.3
    ) {
      this.setNoBuy()
    }
    if (
      this.btbIndexTotalInfo.positionFen < 50 &&
      (dayEmaInfo.days5RealWave < 0.3 && dayEmaInfo.isBuffer && dayEmaInfo.isReadjust) &&
      !dayEmaInfo.isDuo &&
      (hour4EmaInfo.nowRatio5 > 0.5 && hour4EmaInfo.isBuffer && hour4EmaInfo.isLongKong) &&
      hourEmaInfo.nowRatio5 > 0.5
    ) {
      this.setNoBuy()
    }
  },
  noBuySellIfNoLong () {
    const weekEmaInfo = this.btbItem.weekEmaInfo || {}
    const dayEmaInfo = this.btbItem.dayEmaInfo || {}
    const hour4EmaInfo = this.btbItem.hour4EmaInfo || {}
    const hourEmaInfo = this.btbItem.hourEmaInfo || {}
    const minute15EmaInfo = this.btbItem.minute15EmaInfo || {}
    if (!(
      (weekEmaInfo.ema10 > weekEmaInfo.ema20 && weekEmaInfo.ema5 > weekEmaInfo.ema10) &&
      (dayEmaInfo.ema10 > dayEmaInfo.ema20 && dayEmaInfo.ema5 > dayEmaInfo.ema10) &&
      (hour4EmaInfo.ema10 > hour4EmaInfo.ema20 && hour4EmaInfo.ema5 > hour4EmaInfo.ema10) &&
      (hourEmaInfo.ema10 > hourEmaInfo.ema20 && hourEmaInfo.ema5 > hourEmaInfo.ema10) &&
      (minute15EmaInfo.ema10 > minute15EmaInfo.ema20 && minute15EmaInfo.ema5 > minute15EmaInfo.ema10)
    )) {
      this.setNoBuy()
    }
    if (!(
      (weekEmaInfo.ema10 < weekEmaInfo.ema20 && weekEmaInfo.ema5 < weekEmaInfo.ema10) &&
      (dayEmaInfo.ema10 < dayEmaInfo.ema20 && dayEmaInfo.ema5 < dayEmaInfo.ema10) &&
      (hour4EmaInfo.ema10 < hour4EmaInfo.ema20 && hour4EmaInfo.ema5 < hour4EmaInfo.ema10) &&
      (hourEmaInfo.ema10 < hourEmaInfo.ema20 && hourEmaInfo.ema5 < hourEmaInfo.ema10) &&
      (minute15EmaInfo.ema10 < minute15EmaInfo.ema20 && minute15EmaInfo.ema5 < minute15EmaInfo.ema10)
    )) {
      this.setNoSell()
    }
  }
}
